package bayes.traversal;

import text.Text;
import text.TextSet;
import text.traversal.ComputeProbaClass;
import text.traversal.ComputeProbaOccurence;
import text.traversal.ITextTraversal;

/**
 * log(p(c|d) = log(p(d|c)) + log(p(c))
 */
public class ComputeProbaClassOwner implements ITextTraversal
{
	private final static String PROBA_CLASS_OWNER = "ProbaClassOwner";
	
	private final TextSet textSet;
	private final String infoName;
	
	public String getInfoName()
	{
		return this.infoName;
	}
	
	private double proba;
	
	public void reset()
	{
		this.proba = 0;
	}
	
	public static String getInfoName(TextSet textSet)
	{
		return PROBA_CLASS_OWNER + textSet.getName();
	}
	
	public void traversal(String word, Text text)
	{		
		double occurenceCount = text.getOccurenceCount(word);
		Double textProba = textSet.getInfo(ComputeProbaOccurence.PROBA_OCCURENCE, word);
		if(textProba == null)
		{
			return;
		}
		
		proba += occurenceCount * Math.log10(textProba);
	}
	
	public double saveProbaClass(Text text)
	{
		proba += Math.log10(textSet.getInfo(ComputeProbaClass.PROBA_CLASS));
		text.addInfo(infoName, proba);

		return proba;
	}
	
	public ComputeProbaClassOwner(TextSet textSet)
	{
		this.reset();
		this.textSet = textSet;
		this.infoName = getInfoName(textSet);
	}

}
